package org.come.bean;

import java.awt.Color;
import java.awt.Graphics;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.come.entity.Goodstable;
import org.come.entity.RoleSummoning;
import org.come.model.Lingbao;
import org.come.until.GoodsListFromServerUntil;

import com.updateNew.MyIsif;
//交易的主界面
/** 交易的货物 */
public class GoodTrans {

    /** 准备交易的物品 */
    private List<Goodstable> goods;
    /** 准备交易的召唤兽 */
    private List<RoleSummoning> pets;
    /** 准备交易的灵宝 */
    private List<Lingbao> lingbaos;
    /** 准备交易的金钱 */
    private BigDecimal money;

    /** 物质刷新 */
    public void updateGood(GoodTrans2 goodTrans2) {
        setGood(goodTrans2.getGoods());
        setPet(goodTrans2.getPet(), goodTrans2.isI());
        setLingbao(goodTrans2.getLingbao(), goodTrans2.isI());
        if (goodTrans2.getMoney() != null) {
            this.money = goodTrans2.getMoney();
        }
    }

    // 0是左边 1是右边
    public void paint(Graphics g, int type) {
        g.setColor(Color.white);
        	if (goods != null) {
                for (int i = 0; i < goods.size(); i++) {
                    int x = 20 + i % 4 * 51;
                    int y = 357 + i / 4 * 51;
                    if (type == 1) {
                        x += 270;
                    }
                    g.drawImage(GoodsListFromServerUntil.imgpath(goods.get(i).getSkin()).getImage(), x + 23, y - 9,49,49, null);
                    g.drawString(goods.get(i).getUsetime().toString(), x + 4 + 23, y + 13 - 13);
                }
            }
            if (pets != null) {
                for (int i = 0; i < pets.size(); i++) {
                    if (type == 0) {
                        g.drawString(pets.get(i).getSummoningname(), 20 + 23 + i / 2 * 110, 330 - 13 + i % 2 * 20);
                    } else {
                        g.drawString(pets.get(i).getSummoningname(), 290 + 23 + i / 2 * 110, 330 - 13 + i % 2 * 20);
                    }
                }
            }
            if (lingbaos != null) {
                for (int i = 0; i < lingbaos.size(); i++) {
                    if (type == 0) {
                        g.drawString(lingbaos.get(i).getBaoname(), 20 + 23 + (i + 2) / 2 * 110, 330 - 13 + i % 2 * 20);
                    } else {
                        g.drawString(lingbaos.get(i).getBaoname(), 290 + 23 + (i + 2) / 2 * 110, 330 - 13 + i % 2 * 20);
                    }
                }
            }
    }

    public List<Goodstable> getGoods() {
        return goods;
    }

    public void setGoods(List<Goodstable> goods) {
        this.goods = goods;
    }

    public void setGood(Goodstable good) {
        if (good == null) {
            return;
        }
        if (goods == null) {
            goods = new ArrayList<>();
        }
        for (int i = goods.size() - 1; i >= 0; i--) {
            if (goods.get(i).getRgid().compareTo(good.getRgid()) == 0) {
                if (good.getUsetime() <= 0) {
                    goods.remove(i);
                } else {
                    goods.set(i, good);
                }
                return;
            }
        }
        if (good.getUsetime() > 0) {
            goods.add(good);
        }
    }

    public List<RoleSummoning> getPets() {
        return pets;
    }

    public void setPets(List<RoleSummoning> pets) {
        this.pets = pets;
    }

    public void setPet(RoleSummoning pet, boolean is) {
        if (pet == null) {
            return;
        }
        if (pets == null) {
            pets = new ArrayList<>();
        }
        if (is) {
            pets.add(pet);
        } else {
            for (int i = pets.size() - 1; i >= 0; i--) {
                if (pets.get(i).getSid().compareTo(pet.getSid()) == 0) {
                    pets.remove(i);
                    return;
                }
            }
        }
    }

    public List<Lingbao> getLingbaos() {
        return lingbaos;
    }

    public void setLingbaos(List<Lingbao> lingbaos) {
        this.lingbaos = lingbaos;
    }

    public void setLingbao(Lingbao lingbao, boolean is) {
        if (lingbao == null) {
            return;
        }
        if (lingbaos == null) {
            lingbaos = new ArrayList<>();
        }
        if (is) {
            lingbaos.add(lingbao);
        } else {
            for (int i = lingbaos.size() - 1; i >= 0; i--) {
                if (lingbaos.get(i).getBaoid().compareTo(lingbao.getBaoid()) == 0) {
                    lingbaos.remove(i);
                    return;
                }
            }
        }
    }

    public BigDecimal getMoney() {
        return money;
    }

    public void setMoney(BigDecimal money) {
        this.money = money;
    }

    public Goodstable getGood(BigDecimal rgid) {
        if (goods == null) {
            return null;
        }
        for (int i = 0; i < goods.size(); i++) {
            if (goods.get(i).getRgid().compareTo(rgid) == 0) {
                return goods.get(i);
            }
        }
        return null;
    }

    public Goodstable getGood(int p) {
        if (goods == null || goods.size() <= p) {
            return null;
        }
        return goods.get(p);
    }

    public RoleSummoning getPet(int p) {
        if (pets == null || pets.size() <= p) {
            return null;
        }
        return pets.get(p);
    }

    public Lingbao getLingbao(int p) {
        if (lingbaos == null || lingbaos.size() <= p) {
            return null;
        }
        return lingbaos.get(p);
    }
}
